﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    public partial class FrmListStudent : Form
    {
        SqlConnection conn;
        public static string studentcode;
        public FrmListStudent()
        {
            InitializeComponent();
        }

        private void LoadComboFaculty()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select * from TblFaculty";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cbFaculty.DataSource = dt;
            cbFaculty.DisplayMember = "Faculty_Name";
            cbFaculty.ValueMember = "Faculty_Code";
            cbFaculty.Text = "Select faculty";
        }
        private void LoadComboClass()
        {
            string fac = cbFaculty.SelectedValue.ToString();
            conn = Connect.getConnection();
            conn.Open();
            string sql = "Select * from TblClass where Faculty_Code='" + fac + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            da.Dispose();
            cmClass.DataSource = dt;
            cmClass.DisplayMember = "Class_Code";
            cmClass.ValueMember = "Class_Code";
            conn.Close();
            cmClass.Text = "Select class";
            
        }

        private void FrmListStudent_Load(object sender, EventArgs e)
        {
            LoadComboFaculty();
        }

        private void cmClass_SelectionChangeCommitted(object sender, EventArgs e)
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select Student_Code,Class_Code,Faculty_Name,Student_Name,Student_DOB,Student_Sex,Current_Address from TblStudents where Class_Code='" + cmClass.SelectedValue + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgListStudent.DataSource = dt;
            
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (dgListStudent.DataSource == null)
            {
                MessageBox.Show("Please select one row");
            }
            else
            {
                studentcode = dgListStudent.CurrentRow.Cells[0].Value.ToString();
                FormView FormView = new FormView();
                FormView.Show();
                //this.Hide();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgListStudent.DataSource == null)
            {
                MessageBox.Show("Please select one row");
            }
            else
            {
                studentcode = dgListStudent.CurrentRow.Cells[0].FormattedValue.ToString();
                FormInfo_Student FormInfo_Student = new FormInfo_Student();
                FormInfo_Student.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dgListStudent.DataSource == null)
            {
                MessageBox.Show("Please select one row");
            }
            else
            {
                studentcode = dgListStudent.CurrentRow.Cells[0].FormattedValue.ToString();
                FormAdd_Mark FormAdd_Mark = new FormAdd_Mark();
                FormAdd_Mark.Show();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgListStudent.DataSource == null)
            {
                MessageBox.Show("Please select one row");
            }
            else
            {
                if (MessageBox.Show("Are you sure?", "You realy want to delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string stucod = dgListStudent.CurrentRow.Cells[0].FormattedValue.ToString();
                    conn = Connect.getConnection();
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("delete from TblStudents where Student_Code='" + stucod + "'", conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            FormMain FormMain = new FormMain();
            FormMain.Show();
            this.Hide();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormAdd_Student FormAdd_Student = new FormAdd_Student();
            FormAdd_Student.Show();
        }

        private void cbFaculty_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadComboClass();
        }


    }
}
